home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / batches / unixopt.bat < prev   
DOS Batch File  |  1993-10-24  |  3KB  |  95 lines

  1. @echo off
  2. !  unixopt.bat    Batch file to parse a UNIX-like option
  3. !
  4. !  An option as parsed by unixopt.bat consists of (from left to right):
  5. !        a dash as first character
  6. !        a character which identifies the option
  7. !        a string
  8. !
  9. !  Call unixopt as follows:
  10. !
  11. !            unixopt what err opt rest
  12. !
  13. !  where:    what   = string to be parsed
  14. !            err    = name of the variable into which unixopt is to return an error code
  15. !            opt    = name of the variable into which unixopt is to return the option
  16. !                     character
  17. !            rest   = name of the variable into which unixopt is to return the rest of
  18. !                     'what' after removing dash and option
  19. !
  20. !  unixopt returns into 'err' the following codes:
  21. !      0 = no error
  22. !      1 = 'what' consists of a dash and an option character without any additional
  23. !          string. In this case, unixopt sets 'opt' correctly and 'rest' to a single
  24. !          space.
  25. !     -1 = 'what' does not begin with a dash
  26. !     -2 = 'what' consists of a dash without any option character.
  27. !     -3 = unixopt was called with 2 or 3 parameters instead of 4.
  28. !
  29. !  If unixopt is called with less than 2 parameters, it returns without any action.
  30. !
  31. !  Copyright © 1993 by Rainbow Hill Pty Ltd. All rights reserved.
  32. !
  33.  
  34. ! check for the presence of the parameters
  35. if "%1x" == "x" goto RET_LBL
  36. if "%2x" == "x" goto RET_LBL
  37. if "%3x" == "x" goto NOPAR_LBL
  38. if "%4x" == "x" goto NOPAR_LBL
  39.  
  40. ! check for the presence of the dash at the beginning of par 1
  41. ! The 'x' is needed to ensure that 'tmp1' is not numeric and that it consists of
  42. ! at least two characters, so that 'decr' provides predictable results.
  43. set tmp1=%1x
  44. ! let's remove the first character (ie. the dash) from 'tmp1'
  45. decr -tmp1
  46. ! and now we remove the "dashless" first parameter from a full copy of it, so that
  47. ! we are left with the dash itself
  48. ! The 'y' is needed to prevent 'decr' from removing 'tmpt2' when the dash is not
  49. ! followed by any character.
  50. set tmp2=y%1x
  51. decr tmp2 by %tmp1%
  52. if not %tmp2% == y- goto NODASH_LBL
  53.  
  54. ! check for the presence of something after the dash
  55. if %tmp1% == x goto NOOPT_LBL
  56.  
  57. ! isolate the option character
  58. ! Now 'tmp1' contains at least one character (ie. the option character) followed
  59. ! by the 'x' that we have appended to %1 when we created 'tmp1'.
  60. set tmp2=%tmp1%
  61. !let's remove the first character (ie. the option character) from 'tmp1'
  62. decr -tmp1
  63. ! here we do what we did to isolate the dash, but we do not need to prepend a 'y'
  64. ! because we know that 'tmp2' contains at least two characters and 'tmp1' one
  65. decr tmp2 by %tmp1%
  66. ! we are now ready to return the option character
  67. set %3=%tmp2%
  68.  
  69. ! check for the presence of something after the option character
  70. if %tmp1% == x goto NOREST_LBL
  71.  
  72. ! there is still something left
  73. ! let's get rid of the 'x' and return the rest
  74. decr tmp1
  75. set %4=%tmp1%
  76.  
  77. !return successfully
  78. set %2=0
  79. goto RET_LBL
  80.  
  81. :NOREST_LBL
  82.   set %2=1
  83.   goto RET_LBL
  84. :NODASH_LBL
  85.   set %2=-1
  86.   goto RET_LBL
  87. :NOOPT_LBL
  88.   set %2=-2
  89.   goto RET_LBL
  90. :NOPAR_LBL
  91.   set %2=-3
  92. :RET_LBL
  93.   set tmp1=
  94.   set tmp2=
  95.